var MaterialModel = function (material) {
  BaseModel.call(this);

  var user = window.sessionStorage.getItem('user');
  user = JSON.parse(user);

  var self = this;
  var imgFields = ['img', 'imgTwo', 'imgThree', 'imgFour'];
  var fn = {
    initData: function (material) {
      if (material === null) {
        material = {
          providerName: user.company,
          isSynchronization: 2,
          defaultWarehouse: '原材料仓'
        };
      }

      var fields = ['id', 'materialName', 'model', 'color', 'colorDescription', 'unit', 'unitPrice', 'defaultWarehouse',
        'providerNo', 'providerName', 'company', 'material', 'providerName', 'isSynchronization', 'count', 'contactPhone',
        'procurementCycles', 'contactName', 'status', 'materialCode'
      ];
      self.setDataFromObj(material, fields, '');

      if (self.unitPrice === '' || self.unitPrice === 0) {
        self.unitPrice = undefined;
      }

      if (self.count === '' || self.count === 0) {
        self.count = undefined;
      }

      if (self.isSynchronization === '') {
        self.isSynchronization = 2;
      }

      var classify = material.classify;
      if (classify === undefined) {
        classify = [];
      }
      var classifyIds = _.map(classify, 'id');
      self.classifyIds = classifyIds;
      self.classifyList = classify;

      var imgUrls = material.imgUrls;
      if (SysUtil.isEmpty(imgUrls)) {
        imgUrls = [];
        if (material !== null) {
          for (var i = 0; i < imgFields.length; i++) {
            var imgField = imgFields[i];
            var imgUrl = material[imgField];
            if (!SysUtil.isEmpty(imgUrl)) {
              imgUrls.push(imgUrl);
            }
          }
        }
      }
      self.imgUrls = imgUrls;
    }
  };

  this.checkValid = function () {
    var fields = ['materialName', 'model', 'providerName', 'unit', 'defaultWarehouse'];
    return CheckVaildUtil.checkNullField(fields, this);
  };

  this.setUpdateAuthority = function (fields) {
    var updateAuthority = {};
    for (var j = 0; j < fields.length; j++) {
      var field = fields[j];
      var value = this[field];
      updateAuthority[field] = SysUtil.isEmpty(value);
    }
    this.updateAuthority = updateAuthority;
  };

  this.getObject = function (type) {
    var postData = {};
    var fields = ['materialName', 'model', 'color', 'colorDescription', 'unit', 'defaultWarehouse',
      'providerNo', 'material', 'providerName', 'isSynchronization'
    ];
    var numFields = ['unitPrice', 'procurementCycles'];
    if (type === 'import') {
      fields.pushArray(['company', 'materialCode']);
      numFields.pushArray(['safetyStockMax', 'safetyStockMin']);
      postData.status = '使用';
      postData.isStock = 1;

      var isEquals;
      if (this.unit === '付') {
        isEquals = 1;
      } else {
        isEquals = 0;
      }
      postData.isEquals = isEquals;
    }

    if (this.isSynchronization !== 1) {
      numFields.push('count');
    }

    this.setPostData(postData, fields);
    for (var i = 0; i < numFields.length; i++) {
      var numField = numFields[i];
      var value = this[numField];
      if (value === undefined) {
        value = 0;
      }
      postData[numField] = value;
    }

    var imgUrls = this.imgUrls;
    for (var i = 0; i < imgFields.length; i++) {
      var imgField = imgFields[i];
      var imgUrl = imgUrls[i];
      if (imgUrl === undefined) {
        imgUrl = '';
      }
      postData[imgField] = imgUrl;
    }

    var classifyIds = this.classifyIds;
    if (classifyIds.length !== 0) {
      postData.typeId = classifyIds[classifyIds.length - 1];
    }

    return postData;
  };

  this.getUpdateObject = function () {
    var postData = this.getObject();
    postData.id = this.id;
    postData.status = this.status;
    postData.materialCode = this.materialCode;
    return postData;
  };

  fn.initData(material);
}